/**
 * Created by lx on 2017/3/15.
 */
angular.module('module.homepage', [
    'ngCookies',
])

    .config(['$stateProvider', function ($stateProvider) {
            $stateProvider
                .state('homepage', {
                    url: '/homepage?access&userid&shopid',
                    templateUrl: 'view/home/homepage.html',
                    controller: 'HomepageController'
                })
                .state('sharetemplate', {
                    url: '/sharetemplate?access&userid&shopid&templateid',
                    templateUrl: 'view/home/sharetemplate.html',
                    controller: 'SharetemplateController'
                })

        }])

    .controller('HomepageController', ['$scope', '$state','$stateParams','TemplateService',
        function ($scope,$state,$stateParams,TemplateService) {
            //查看是否有下级
            $scope.templates = [];
            if ($stateParams.access) {
                TemplateService.gettemplate($stateParams.access).$promise.then(function (data) {
                    for (var i = 0; i < data.length; i++) {
                        if (data[i].templateid) {
                            TemplateService.gettemplatedetail(data[i].templateid, function (success) {
                                $scope.templates.push(success);
                            }, function (error) {
                                console.log(error);
                            })
                        }
                    }
                })
            } else {
                console.log("error")
            }
            //});
            $scope.mysharetpl = function (templateid) {
                $state.go('sharetemplate', {
                    templateid: templateid,
                    access: $stateParams.access,
                    shopid: $stateParams.shopid,
                    userid: $stateParams.userid
                });
            }
    }])
    .controller('SharetemplateController', ['$scope', '$state','$timeout','$stateParams','TemplateService','AuthService',
        function ($scope,$state,$timeout,$stateParams,TemplateService,AuthService) {
        //模态框显示
        $scope.usershow=false;
        $scope.congratulation=false;
        TemplateService.gettemplatedetail($stateParams.templateid).$promise.then(function(data){
            $scope.share=data;
        },function(error){
            console.log(error);
        });
        var shop={
            userId:$stateParams.userid,
            shopId:$stateParams.shopid,
            access:$stateParams.access,
            templateId:$stateParams.templateid,
        };
        var sharebtn= document.getElementById("goshare");
        $scope.gomyshare=function(){
            sharebtn.setAttribute("disabled", "disabled");
            $(sharebtn).addClass("myshare_btn_hover");
            //生成invite
            AuthService.getinvite(shop,function(date){
                $scope.congratulation=true;
                $scope.usershow=true;
                $timeout(function(){
                    $state.go('invite',{linkid:date.linkId,access:shop.access,userid:shop.userId})
                }, 2000);
            },function(error){
                console.log(error)
            })
        };
        //模态框点击消失
            $scope.fadeout = function () {
                $("#share_my").fadeOut(1000, function () {
                    $scope.usershow = false;
                    $scope.congratulation = false;
                });
                $("#share_you").fadeOut(1000, function () {
                    $scope.usershow = false;
                    $scope.congratulation = false;
                });
            }
    }])





